package com.woniuxy.sales.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.sales.mapper.CategoriesMapper;
import com.woniuxy.sales.mapper.ProductsMapper;
import com.woniuxy.sales.mapper.UsersMapper;
import com.woniuxy.sales.model.entity.Categories;
import com.woniuxy.sales.model.entity.Products;
import com.woniuxy.sales.model.entity.Reviews;
import com.woniuxy.sales.model.entity.Users;
import com.woniuxy.sales.model.vo.ReviewsVo;
import com.woniuxy.sales.service.ReviewsService;
import com.woniuxy.sales.mapper.ReviewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【评论表】
*/
@Service
public class ReviewsServiceImpl extends ServiceImpl<ReviewsMapper, Reviews> implements ReviewsService{

    @Autowired
    private ReviewsMapper reviewsMapper;
    @Autowired
    private ProductsMapper productsMapper;
    @Autowired
    private CategoriesMapper categoriesMapper;
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public List<ReviewsVo> query(Integer userId) {
        LambdaQueryWrapper<Reviews> wrapper = new LambdaQueryWrapper<>();
        //查询某个用户的评论
        wrapper.eq(Reviews::getUserId, userId);
        //按评论时间倒序排列
        wrapper.orderByDesc(Reviews::getReviewDate);
        List<Reviews> reviews = reviewsMapper.selectList(wrapper);
        List<ReviewsVo> reviewsVos = reviews.stream().map(rev -> {//每一个收藏的商品
            ReviewsVo reviewsVo = BeanUtil.copyProperties(rev, ReviewsVo.class);
            //根据商品id得到商品详情
            Products products = productsMapper.selectById(rev.getProductId());
            //由商品id得到类型，从而得到类型名称
            Categories categories = categoriesMapper.selectById(products.getCategoryId());
            reviewsVo.setCategoryName(categories.getCategoryName());
            BeanUtil.copyProperties(products, reviewsVo);
            return reviewsVo;
        }).collect(Collectors.toList());
        return reviewsVos;
    }

    //删除评论
    @Override
    public void remove(Integer id) {
        LambdaQueryWrapper<Reviews> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Reviews::getReviewId,id);
        reviewsMapper.delete(wrapper);
    }

    @Override
    public void submitReview(Reviews reviews, Integer userId) {
       reviews.setUserId(userId);
       reviews.setReviewDate(new Date());
       reviewsMapper.insert(reviews);
    }

    @Override
    public List<Reviews> getReviewsByCarId(Integer productId) {
        LambdaQueryWrapper<Reviews> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Reviews::getProductId,productId);
        wrapper.orderByDesc(Reviews::getReviewDate);
        return reviewsMapper.selectList(wrapper);
    }

    @Override
    public List<Users> getReviewsByUserId(Integer userId) {
        LambdaQueryWrapper<Users> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Users::getUserId,userId);
        return usersMapper.selectList(wrapper);
    }


}




